package com.dongdong.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdong.pojo.TbItemCat;
import com.dongdong.pojo.TbTypeTemplate;
import com.dongdong.service.ItemService;
import com.dongdong.service.TypeTemplateService;
import com.dongdongshop.configresult.Result;
import com.dongdongshop.em.ResultEnum;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("itemcontroller")
public class ItemController {


    @Reference
    private TypeTemplateService typeTemplateService;

    @Reference
    private ItemService itemService;

    @RequestMapping("select")
    @ResponseBody
    public Result select(){
        List<TbItemCat> tbItemCats = itemService.selectAll();
        if(tbItemCats == null || tbItemCats.size()  <= 0){
            return Result.builResult(ResultEnum.ERROR);
        }
        return Result.builResult(ResultEnum.SUCCESS).setResout(tbItemCats);
    }

    @RequestMapping("selectPid")
    @ResponseBody
    public Result selectPid(Long id){
        List<TbItemCat> itemCatList = itemService.selectPid(id);

        return  Result.builResult(ResultEnum.SUCCESS).setResout(itemCatList);
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbItemCat itemCatc){
        boolean b = itemService.insert(itemCatc);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(Long id){
        boolean b = itemService.delete(id);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

    @RequestMapping("toupdate")
    @ResponseBody
    public Result toupdate(Long id){
        TbItemCat tbItemCat = itemService.selectId(id);
        if(tbItemCat != null){
            return Result.builResult(ResultEnum.SUCCESS).setResout(tbItemCat);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

    @RequestMapping("mubna")
    @ResponseBody
    public Result mubna(){
        List<TbTypeTemplate> tbTypeTemplates  = typeTemplateService.selecta();
        if(tbTypeTemplates == null || tbTypeTemplates.size() <= 0){
            return Result.builResult(ResultEnum.ERROR);
        }
        return Result.builResult(ResultEnum.SUCCESS).setResout(tbTypeTemplates);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbItemCat tbItemCat){
        boolean b = itemService.update(tbItemCat);
        if(b){
            return Result.builResult(ResultEnum.SUCCESS);
        }
        return Result.builResult(ResultEnum.ERROR);
    }

}
